package model;

import java.util.Arrays;
import java.util.List;

import org.mybeans.dao.DAOException;
import org.mybeans.factory.BeanFactory;
import org.mybeans.factory.BeanFactoryException;
import org.mybeans.factory.BeanTable;
import org.mybeans.factory.MatchArg;
import org.mybeans.factory.RollbackException;
import org.mybeans.factory.Transaction;

import dataBeans.Bookmark;
import exceptions.MyWebsiteException;

public class BookmarkDAO {
	private BeanFactory<Bookmark> bookmarksFactory;

	public BookmarkDAO() throws DAOException{
		try {
				BeanTable<Bookmark> bookmarkTable = BeanTable.getInstance(Bookmark.class,"mkhani_bookmark");

			if (!bookmarkTable.exists()){ 
				bookmarkTable.create("bookmarkId");
			}
			bookmarkTable.setIdleConnectionCleanup(true);
			bookmarksFactory = bookmarkTable.getFactory();
		} catch (BeanFactoryException e) {
			throw new DAOException(e);
		}
	}
	
	public BookmarkDAO(String jdbcDriver, String jdbcURL, String tableName){
		try{
			BeanTable<Bookmark> bookmarkTable = BeanTable.getSQLInstance(Bookmark.class, tableName, jdbcDriver, jdbcURL);
			if(!bookmarkTable.exists()){
				bookmarkTable.create("bookmarkId");
			}
			bookmarksFactory = bookmarkTable.getFactory();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public BookmarkDAO(String jdbcDriverName, String jdbcURL, String tableName, String dbUsername, String dbPassword){
		try{
			BeanTable<Bookmark> bookmarkTable =  BeanTable.getSQLInstance(Bookmark.class, tableName, jdbcDriverName, jdbcURL, dbUsername, dbPassword);
			if(!bookmarkTable.exists()){
				bookmarkTable.create("bookmarkId");
			}
			bookmarksFactory = bookmarkTable.getFactory();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public Bookmark[] getUserBookmarks(int userId) throws MyWebsiteException{
		try{
			Bookmark bookmarkList[] = bookmarksFactory.match(MatchArg.equals("userId", userId));
			return bookmarkList;
		}catch(RollbackException e){
			e.printStackTrace();
			throw new MyWebsiteException("can't login");
		}
	}
	
	public Bookmark addBookmark(int userId, String URL, String comment) throws MyWebsiteException{
		try{
			Transaction.begin();
			Bookmark bookmarkDb = bookmarksFactory.create();
			bookmarkDb.setUserId(userId);
			bookmarkDb.setClickCount(0);
			bookmarkDb.setUrl(URL);
			bookmarkDb.setComment(comment);
			Transaction.commit();
		}catch(Exception e){
			e.printStackTrace();
			throw new MyWebsiteException("");
		}
		return new Bookmark(userId, URL, comment, 0);
	}
	
	public Bookmark[] checkBookmark(int userId, String url) throws MyWebsiteException{
		try{
			Bookmark bookmarkList[] = bookmarksFactory.match(MatchArg.and(MatchArg.equals("userId", userId), MatchArg.equals("url", url)));
			return bookmarkList;
		}catch(Exception e){
			e.printStackTrace();
			throw new MyWebsiteException("couldn't check bookmark existance");
		}
	}
	
	public Bookmark updateBookmarkClick(int bookmarkId)throws MyWebsiteException{
		try{
			Transaction.begin();
			Bookmark bookmarkCountUpdate = bookmarksFactory.lookup(bookmarkId);
			bookmarkCountUpdate.setClickCount(bookmarkCountUpdate.getClickCount() + 1);
			Transaction.commit();
			return bookmarkCountUpdate;
		}catch(RollbackException e){
			e.printStackTrace();
			throw new MyWebsiteException("can't get bookmark");
		}
	}
	
	public Bookmark updateBookmarkClick(String url)throws MyWebsiteException{
		try{
			Transaction.begin();
			Bookmark [] bookmarkCountUpdate = bookmarksFactory.match(MatchArg.equals("url", url));
			bookmarkCountUpdate[0].setClickCount(bookmarkCountUpdate[0].getClickCount() + 1);
			Transaction.commit();
			return bookmarkCountUpdate[0];
		}catch(RollbackException e){
			e.printStackTrace();
			throw new MyWebsiteException("can't get bookmark");
		}
	}
	
	public List<Bookmark> getAllUserBookmarks() throws MyWebsiteException{
		try{
			List<Bookmark> allUsersBookmarks = Arrays.asList(bookmarksFactory.match());
			return allUsersBookmarks;
		}catch(RollbackException e){
			e.printStackTrace();
			throw new MyWebsiteException("can't get bookmark");
		}
	}
	
	
	public Bookmark deleteBookmark(String url, int userId) throws MyWebsiteException{
		try{
			
			Bookmark bookmarkList[] = bookmarksFactory.match(MatchArg.and(MatchArg.equals("userId", userId), MatchArg.equals("url", url)));
			if(bookmarkList != null && bookmarkList.length > 0){
				System.out.println("Trying to delete from deleteBookmark");
				bookmarksFactory.delete(bookmarkList[0].getBookmarkId());
				return bookmarkList[0];
			}else{
				throw new MyWebsiteException("there is no bookmarks to delete");
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new MyWebsiteException("Throw exception from beans");
		}
	}
}
